package cn.ffcs.uec.selfstaff.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.staff.action.bean.UecCtrtEditBean;
import cn.ffcs.uec.staff.model.UecCtrt;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;

@Controller
@Scope("prototype")
public class UecSelfCtrtEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecCtrtEditBean bean = new UecCtrtEditBean();
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 人员信息
	 */
	private UecStaffInfo uecStaffInfo;
	/**
	 * 添加教育经历
	 */
	private UecCtrt uecCtrt;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化窗口
	 * 
	 * @throws ParseException
	 */
	public void onCreate$uecCtrtEditWindow() throws ParseException {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {
		
		List<NodeVo> nodeVoIshiskeypsnList = new ArrayList<NodeVo>();
		String[][] ishiskeypsn = { { "Y", "是" }, { "N", "否" } };
		for (int i = 0; i < ishiskeypsn.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(ishiskeypsn[i][j]);
				nodeVo.setName(ishiskeypsn[i][j + 1]);
			}
			nodeVoIshiskeypsnList.add(nodeVo);
		}
		// 是否需要试用
		ListboxUtils.rendererForEdit(bean.getIfprop(), nodeVoIshiskeypsnList);
		// 是否提交
		ListboxUtils.rendererForEdit(bean.getIsrefer(), nodeVoIshiskeypsnList);
		// 是否合同模块填写
		ListboxUtils.rendererForEdit(bean.getIfwrite(), nodeVoIshiskeypsnList);

		// 业务类型
		List<NodeVo> nodeVoConttypeList = new ArrayList<NodeVo>();
		String[][] conttype = { { "1", "签订" }, { "2", "续签" }, { "3", "变更" },
				{ "4", "解除" }, { "4", "终止" }, { "5", "中止" } };
		for (int i = 0; i < conttype.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(conttype[i][j]);
				nodeVo.setName(conttype[i][j + 1]);
			}
			nodeVoConttypeList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getConttype(), nodeVoConttypeList);
		
		//合同模板
		List<NodeVo> nodeVoModelList = new ArrayList<NodeVo>();
		String[][] model = { { "rKp7GvgdmLQ=", "固定合同" }, { "~", "无" }};
		for (int i = 0; i < model.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(model[i][j]);
				nodeVo.setName(model[i][j + 1]);
			}
			nodeVoModelList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getContmodel(), nodeVoModelList);
		
		//解除提出方presenter
		List<NodeVo> nodeVoPresenterList = new ArrayList<NodeVo>();
		String[][] presenter = { { "1", "用人单位" }, { "2", "劳动者" }};
		for (int i = 0; i < presenter.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(presenter[i][j]);
				nodeVo.setName(presenter[i][j + 1]);
			}
			nodeVoPresenterList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getPresenter(), nodeVoPresenterList);
		
		//试用期限单位propUnit
		List<NodeVo> nodeVoPropUnitList = new ArrayList<NodeVo>();
		String[][] propUnit = { { "1", "年" }, { "2", "月" }, { "3", "日" }};
		for (int i = 0; i < propUnit.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(propUnit[i][j]);
				nodeVo.setName(propUnit[i][j + 1]);
			}
			nodeVoPropUnitList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getPropUnit(), nodeVoPropUnitList);
		// 合同期限单位
		ListboxUtils.rendererForEdit(bean.getContUnit(), nodeVoPropUnitList);
		
		
		//// 合同期限类型
		List<NodeVo> nodeVoTermtypeList = new ArrayList<NodeVo>();
		String[][] termtype = { { "CM01", "固定期限" }, { "CM02", "无固定期限" }};
		for (int i = 0; i < termtype.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(termtype[i][j]);
				nodeVo.setName(termtype[i][j + 1]);
			}
			nodeVoTermtypeList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getTermtype(), nodeVoTermtypeList);
	}

	/**
	 * bindBean
	 * 
	 * @throws ParseException
	 */
	public void bindBean() throws ParseException {
		opType = (String) arg.get("opType");
		if ("add".equals(opType)) {
			this.bean.getUecCtrtEditWindow().setTitle("新增人员合同信息");
			bean.getUecCtrtEditDiv().setHeight("40%");
			uecStaffInfo = (UecStaffInfo) arg.get("uecStaffInfo");
		}
		if ("view".equals(opType)) {
			this.bean.getUecCtrtEditWindow().setTitle("查看人员合同信息");
			uecCtrt = (UecCtrt) arg.get("uecCtrt");
			if (null != uecCtrt) {
				PubUtil.fillBeanFromPo(uecCtrt, bean);
				stringTransToDate();
				bean.getButtonDiv().setVisible(false);
				PubUtil.disableComponent(bean.getUecCtrtEditWindow(), true);
			}
		}
		if ("edit".equals(opType)) {
			this.bean.getUecCtrtEditWindow().setTitle("编辑人员合同信息");
			bean.getUecCtrtEditDiv().setHeight("40%");
			uecCtrt = (UecCtrt) arg.get("uecCtrt");
			if (null != uecCtrt) {
				PubUtil.fillBeanFromPo(uecCtrt, bean);
				stringTransToDate();
			}
		}
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		if ("add".equals(opType)) {
			uecCtrt = UecCtrt.newInstance();
			PubUtil.fillPoFromBean(bean, uecCtrt);
			dateTransToString();
			uecCtrt.setStaffId(uecStaffInfo.getStaffId());
			uecCtrt.addOnly();
			Events.postEvent("onOK", this.self, uecCtrt);
			this.onCancel();
			Messagebox.show("新增成功！");
		}
		if ("edit".equals(opType)) {
			PubUtil.fillPoFromBean(bean, uecCtrt);
			dateTransToString();
			uecCtrt.updateOnly();
			Events.postEvent("onOK", this.self, uecCtrt);
			this.onCancel();
			Messagebox.show("更新成功！");
		}
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecCtrtEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		// if (StrUtil.isNullOrEmpty(bean.getEncourName().getValue())) {
		// return "填写奖励名称";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourDate().getValue())) {
		// return "填写奖励日期";
		// }
		return null;
	}

	/**
	 * 将Date类型时间转成String类型 Date --> String 一套模型，两套标准：日期类型需要格式化
	 */
	public void dateTransToString() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		// 合同开始日期
		if (!StrUtil.isNullOrEmpty(bean.getBegindate().getValue())) {
			uecCtrt.setBegindate(formatter.format(bean.getBegindate()
					.getValue()));
		}
		// 合同结束日期
		if (!StrUtil.isNullOrEmpty(bean.getEnddate().getValue())) {
			uecCtrt.setEnddate(formatter.format(bean.getEnddate().getValue()));
		}
		// 实际终止日期
		if (!StrUtil.isNullOrEmpty(bean.getGlbdef1().getValue())) {
			uecCtrt.setGlbdef1(formatter.format(bean.getGlbdef1().getValue()));
		}
		// 试用开始日期
		if (!StrUtil.isNullOrEmpty(bean.getProbegindate().getValue())) {
			uecCtrt.setProbegindate(formatter.format(bean.getProbegindate()
					.getValue()));
		}
		// 试用结束日期
		if (!StrUtil.isNullOrEmpty(bean.getProbenddate().getValue())) {
			uecCtrt.setProbenddate(formatter.format(bean.getProbenddate()
					.getValue()));
		}
		// 业务发生日期
		if (!StrUtil.isNullOrEmpty(bean.getSigndate().getValue())) {
			uecCtrt.setSigndate(formatter.format(bean.getSigndate().getValue()));
		}
	}

	/**
	 * 将String类型的时间转换成Date类型
	 * 
	 * @throws ParseException
	 */
	public void stringTransToDate() throws ParseException {
		// 时间类型转换：String --> Date
		// 合同开始日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (null != uecCtrt.getBegindate()) {
			bean.getBegindate().setValue(sdf.parse(uecCtrt.getBegindate()));
		}
		// 合同结束日期
		if (null != uecCtrt.getEnddate()) {
			bean.getEnddate().setValue(sdf.parse(uecCtrt.getEnddate()));
		}
		// 实际终止日期
		if (null != uecCtrt.getGlbdef1()) {
			bean.getGlbdef1().setValue(sdf.parse(uecCtrt.getGlbdef1()));
		}
		// 试用开始日期
		if (null != uecCtrt.getProbegindate()) {
			bean.getProbegindate().setValue(
					sdf.parse(uecCtrt.getProbegindate()));
		}
		// 试用结束日期
		if (null != uecCtrt.getProbenddate()) {
			bean.getProbenddate().setValue(sdf.parse(uecCtrt.getProbenddate()));
		}
		// 业务发生日期
		if (null != uecCtrt.getSigndate()) {
			bean.getSigndate().setValue(sdf.parse(uecCtrt.getSigndate()));
		}
	}

}
